//
//  TokenViewController.m
//  XRSquare
//
//  Created by UaenaMo on 2018/8/7.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "TokenViewController.h"
#import "FinalStepViewController.h"
#import "ForgetPasswordLastViewController.h"
@interface TokenViewController ()
{
    UITextField * code;
}

@end

@implementation TokenViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    {
        self.fp = NO;
    }
    return self;
}

-(void)tapCancel
{
    [code resignFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView * backImage = [[UIImageView alloc]initWithFrame:self.view.bounds];
    backImage.image = [UIImage imageNamed:@"lb"];
    backImage.userInteractionEnabled = YES;
    [self.view addSubview:backImage];
    
    UITapGestureRecognizer * tapCancel = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCancel)];
    [backImage addGestureRecognizer:tapCancel];
    
    UIButton * backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 20, 40, 40)];
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
    
    UILabel * createAccount = [[UILabel alloc]initWithFrame:CGRectMake(20, 120, ScreenWidth - 40, 30)];
    createAccount.font = [UIFont boldSystemFontOfSize:15];
    createAccount.text = @"验证码";
    [self.view addSubview:createAccount];
    
    code = [[UITextField alloc]initWithFrame:CGRectMake(20, 170, ScreenWidth - 40 - 60, 30)];
    code.placeholder = @"验证码";
    [self.view addSubview:code];
    
    UIView * line2 = [[UIView alloc]initWithFrame:CGRectMake(20, 200, ScreenWidth - 40, 1)];
    line2.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
    [self.view addSubview:line2];
    
    UIButton * next = [[UIButton alloc]initWithFrame:CGRectMake(20, 250, ScreenWidth - 40, 40)];
    next.layer.borderColor = [UIColor orangeColor].CGColor;
    next.layer.borderWidth = 1;
    next.layer.cornerRadius = 20;
    [next setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [next setTitle:@"下一步" forState:UIControlStateNormal];
    [next addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:next];
    
}


-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)nextAction
{
    if (self.fp)
    {
        [NetRequestCenter changePasswordWithS:@"2" mobile:self.phone password:@"" captcha:code.text checktype:@"0" result:^(BOOL success, NSDictionary *resultDic) {
            if (success)
            {
                ForgetPasswordLastViewController * vc = [[ForgetPasswordLastViewController alloc]init];
                vc.phone = self.phone;
                vc.token = code.text;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }];
    }
    else
    {
        [NetRequestCenter registerWithName:self.phone password:nil mobilecode:code.text province:nil city:nil checktype:0 result:^(BOOL success, NSDictionary *resultDic) {

            if (success)
            {
                FinalStepViewController * vc = [[FinalStepViewController alloc]init];
                vc.phone = self.phone;
                vc.token = code.text;
                [self.navigationController pushViewController:vc animated:YES];
            }
            else
            {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:[resultDic objectForKey:@"message"] preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *centain = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
                [alert addAction:centain];
                [self presentViewController:alert animated:YES completion:nil];
            }
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
